<?php

namespace App\Http\Controllers\Frontend;

use App\Http\Controllers\Controller;
use App\Model\InverterData;
use App\Model\InverterDataThree;
use App\Model\InverterDataTwo;
use App\Model\Site;
use App\Model\User;
use Illuminate\Http\Request;
use Maatwebsite\Excel\Facades\Excel;

class UserController extends Controller
{

    public function getProfile(Request $request)
    {

        $user = $this->_getCurrentUser();

        return view('frontend.user.profile', [
            'user' => $user
        ]);
    }

    public function getExportData(Request $request){
        $user = $this->_getCurrentUser();
        $user_id = $user->id;
        $begin = date('Y-m-d H:i:s',strtotime('-2 hour'));
        $end = date('Y-m-d H:i:s');
//        if($user->id<17){
//             $objs = InverterData::where('user_id',$user->id);
//        }else{
//            $objs = InverterDataTwo::where('user_id',$user->id);
//        }
        switch ($user_id){
            case $user_id < 17:
                $objs = InverterData::where('user_id',$user->id);
                break;
            case $user_id >= 17 && $user_id < 43:
                $objs = InverterDataTwo::where('user_id',$user->id);
                break;
            default:
                $objs = InverterDataThree::where('user_id',$user->id);
        }
        if($request->has('from_time')&&$request->has('to_time')){
            $begin = $request->input('from_time');
            $begin = urldecode($begin);
            $end = $request->input('to_time');
            $end = urldecode($end);
        }
        if($request->has('id')){
            $id = $request->input('id');
            if($id !="")$objs = $objs->where('device_id',$id);
        }
        $site = Site::where('user_id',$user->id)->first();
        $site_name = $site?$site->name:'';
        $objs = $objs->whereBetween('EventTime',[$begin,$end])
            ->orderBy('device_id','desc')
            ->orderBy('EventTime','desc')
            ->paginate(20);
        foreach($objs as $obj){
            $obj->site_name = $site_name;
        }
        return view('frontend.user.export',['data'=>$objs]);
    }

    public function getExport(Request $request){
        $user = $this->_getCurrentUser();
        $user_id = $user->id;
        $begin = date('Y-m-d H:i:s',strtotime('-2 hour'));
        $end = date('Y-m-d H:i:s');
        $site = Site::where('user_id',$user->id)->first();
        $site_name = $site?$site->name:'';
//        if($user->id<17) {
//            $objs = InverterData::where('user_id', $user->id);
//        }else{
//            $objs = InverterDataTwo::where('user_id', $user->id);
//        }
        switch ($user_id){
            case $user_id < 17:
                $objs = InverterData::where('user_id', $user->id);
                break;
            case $user_id >= 17 && $user_id < 43:
                $objs = InverterDataTwo::where('user_id',$user->id);
                break;
            default:
                $objs = InverterDataThree::where('user_id',$user->id);
        }
        if($request->has('id')){
            $id = $request->input('id');
            if($id !="")$objs = $objs->where('device_id',$id);
        }
        if($request->has('from_time')&&$request->has('to_time')){
            $begin = $request->input('from_time');
            $begin = urldecode($begin);
            $end = $request->input('to_time');
            $end = urldecode($end);
            if($begin == "")return $this->json_error('请选开始时间','40001');
            if($end == "")return $this->json_error('请选结束时间','40001');
            $objs = $objs->whereBetween('EventTime',[$begin,$end]);
        }
        $objs1 = $objs->orderBy('device_id','desc')
            ->orderBy('EventTime','desc')
            ->get(['id','device_id','DayGenCap','ApparentPower','BoardTemp1','EventTime']);
//        $objs2 = $objs->orderBy('device_id','desc')
//            ->orderBy('EventTime','desc')
//            ->offset(1000)->limit(2000)
//            ->get();
//        $objs3 = $objs->orderBy('device_id','desc')
//            ->orderBy('EventTime','desc')
//            ->offset(5000)->limit(2500)
//            ->get();
//        foreach($objs as $obj){
//            unset($obj->user_id);
//            unset($obj->site_id);
//            unset($obj->created_at);
//            unset($obj->updated_at);
//            unset($obj->deleted_at);
//        }
        $objs1 = $this->__strv($objs1);
//        $objs2 = $this->__strv($objs2);
        Excel::create(date('Y-m-d').'发电数据'.$site_name,function($excel) use ($objs1,$site_name){
            $excel->sheet($site_name, function($sheet) use ($objs1){
                $sheet->fromModel($objs1, false, false, false, false);
//                $sheet->prependRow(array('id','device_id','DCU1','DCI1','DCU2','DCI2','DCU3','DCI3','DCU4','DCI4','ACUA','ACIA','ACUB','ACIB','ACUC','ACIC','IVTemp','TotGenCap','IVStatus','IVError','IVMode','FR','IVRun','YearGenCap','MonthGenCap','LastMonthGenCap','DayGenCap','LastDayGenCap','PowerLimitPercent','ActivePower','ReactivePower','ApparentPower','PowerFactor','BoardTemp1','BoardTemp2','IVType','EventTime','EventType'));
//                $sheet->prependRow(array('字段','设备编号','直流电压1','直流电流1','直流电压2','直流电压2','直流电压3','直流电流3','直流电压4','直流电流4','交流A相电压','交流A相电流','交流B相电压','交流B相电流','交流C相电压','交流C相电流','逆变器温度','TotGenCap','逆变器状态','IVError','IVMode','电网频率','IVRun','年发','月发','上个月发','日发','昨天发','PowerLimitPercent','有功功率','无功功率','视在功率','功率因素','板测温度1','板测温度2','IVType','发生时间','EventType'));
                $sheet->prependRow(array('id','device_id','DayGenCap','ApparentPower','BoardTemp1','EventTime'));
                $sheet->prependRow(array('字段','设备编号','日发','有功功率','板测温度1','发生时间'));
//                foreach($objs1 as $obj){
//                    $sheet->appendRow([$obj->id,$obj->device_id,$obj->DCU1,$obj->DCI1,$obj->DCU2,$obj->DCI2,$obj->DCU3,$obj->DCI3,$obj->DCU4,$obj->DCI4,$obj->ACUA,$obj->ACIA,$obj->ACUB,$obj->ACIB,$obj->ACUC,$obj->ACIC,$obj->IVTemp,$obj->TotGenCap,$obj->IVStatus,$obj->IVError,$obj->IVMode,'FR','IVRun','YearGenCap','MonthGenCap','LastMonthGenCap','DayGenCap','LastDayGenCap','PowerLimitPercent','ActivePower','ReactivePower','ApparentPower','PowerFactor','BoardTemp1','BoardTemp2','IVType','EventTime','EventType']);
//                }
            });
//            $excel->sheet($site_name.'2', function($sheet) use ($objs2){
//                $sheet->fromModel($objs2, false, false, false, false);
//                $sheet->prependRow(array('id','device_id','DCU1','DCI1','DCU2','DCI2','DCU3','DCI3','DCU4','DCI4','ACUA','ACIA','ACUB','ACIB','ACUC','ACIC','IVTemp','TotGenCap','IVStatus','IVError','IVMode','FR','IVRun','YearGenCap','MonthGenCap','LastMonthGenCap','DayGenCap','LastDayGenCap','PowerLimitPercent','ActivePower','ReactivePower','ApparentPower','PowerFactor','BoardTemp1','BoardTemp2','IVType','EventTime','EventType'));
//                $sheet->prependRow(array('字段','设备编号','直流电压1','直流电流1','直流电压2','直流电压2','直流电压3','直流电流3','直流电压4','直流电流4','交流A相电压','交流A相电流','交流B相电压','交流B相电流','交流C相电压','交流C相电流','逆变器温度','TotGenCap','逆变器状态','IVError','IVMode','电网频率','IVRun','年发','月发','上个月发','日发','昨天发','PowerLimitPercent','有功功率','无功功率','视在功率','功率因素','板测温度1','板测温度2','IVType','发生时间','EventType'));
//            });
        })->export('xls');

    }

    public function getChangePassword(Request $request)
    {
        $user = $this->_getCurrentUser();

        return view('frontend.user.change-password', [
            'user' => $user
        ]);
    }

    public function postChangePassword(Request $request)
    {
        $old_password = $request->input('old_password');
        $new_password = $request->input('new_password');

        $user = $this->_getCurrentUser();

        if ($user->password != strtoupper(md5($old_password))) {
            return $this->json_error('原始密码验证失败！', 1001);
        }

        $user->password = strtoupper(md5($new_password));
        if ($user->save()) {
            return $this->json_success();
        } else {
            return $this->json_sys_error();
        }


    }

    private static function __strv($objs){
        foreach($objs as $obj){
            unset($obj->user_id);
            unset($obj->site_id);
            unset($obj->created_at);
            unset($obj->updated_at);
            unset($obj->deleted_at);
        }
        return $objs;
    }
    private  static function  _formatDate($time)
    {
        return date('Y-m-d H:i:s',strtotime($time));
    }


}
